﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using System.Xml.Linq;
using B.MESUI.Login.EquipmentManagement.Model;
using System.Net.Http;
using B.MESUI.Login.EquipmentManagement.DTO;
using Newtonsoft.Json;
using Standard;
using static B.MESUI.Login.EquipmentManagement.DeviceTypeDialog;

namespace B.MESUI.Login.EquipmentManagement.DeviceTypeService
{
    public partial class AddDeviceTypeForm : DevExpress.XtraEditors.XtraForm
    {
        public AddDeviceTypeForm()
        {
            InitializeComponent();
            this.CenterToParent();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            // 非空校验
            if (string.IsNullOrWhiteSpace(textEdit1.Text))
            {
                MessageBox.Show("名称不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textEdit1.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(textEdit2.Text))
            {
                MessageBox.Show("编码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textEdit2.Focus();
                return;
            }
            var model=new B.MESUI.Login.EquipmentManagement.Model.DeviceType();
            model.Code = textEdit1.Text;
            model.Name = textEdit2.Text;
            model.Description = textEdit3.Text;
            model.CreatedUserName = "admin";
            model.Id = "0";
            using (var client = new HttpClient())
            { 
                string url = $"{BaseUrl.Url}DeviceType/Add";
                var jsonStr = JsonConvert.SerializeObject(model);
                var content = new StringContent(jsonStr, Encoding.UTF8, "application/json");
                var response = await client.PostAsync(url, content);
                response.EnsureSuccessStatusCode();
                var json = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<ApiResult<int>>(json);
                MessageBox.Show(result.Message);
                // 保存逻辑...
                this.DialogResult = DialogResult.OK; // 关闭窗口并返回OK
                this.Close();
            }

        }
    }
}